package com.jinchengll.xigua.network.base;

import android.util.Log;

import com.jinchengll.xigua.entity.BaseResponse;
import com.jinchengll.xigua.util.ToastUtil;


import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Rxjava返回结果封装
 *
 * @author jinchengll
 * @since 2021/10/3
 */
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
    private static final String TAG = "BaseObserver";

    @Override
    public void onSubscribe(Disposable d) {
        Log.e(TAG, "onSubscribe: ");
    }

    @Override
    public void onNext(BaseResponse<T> response) {
        //在这边对 基础数据 进行统一处理
        if (response.getCode() == 200) {
            onSuccess(response.getData());
        } else {
            onFailure(response.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {
        //Todo 这里是网络发生了错误，需要单独再做处理
        Log.e(TAG, "Throwable: " + e.getMessage());
        ToastUtil.showShort(e.getMessage());
    }

    @Override
    public void onComplete() {
        Log.e(TAG, "onComplete: ");
    }

    public abstract void onSuccess(T data);

    public abstract void onFailure(String errorMsg);

}
